package com.atguigu.jxc.controller;


import com.atguigu.jxc.domain.ErrorCode;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.Customer;
import com.atguigu.jxc.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/customer")
public class CustomerController {

    @Autowired
    private CustomerService customerService;

//    客户列表分页（名称模糊查询）
//    http://localhost:8080 /customer/list
    @PostMapping("/list")
    public Map<String,Object> list(Integer page, Integer rows, String  customerName){
        return customerService.list(page,rows,customerName);
    }

    //客户添加或修改
    //http://localhost:8080/ customer/save?customerId=1
    @PostMapping("/save")
    public ServiceVO save(Customer customer,String customerId){
        try {
            customerService.save(customer,customerId);
            return new ServiceVO(SuccessCode.SUCCESS_CODE,SuccessCode.SUCCESS_MESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new ServiceVO(ErrorCode.PARA_TYPE_ERROR_CODE, ErrorCode.PARA_TYPE_ERROR_MESS);
        }
    }


    //批量删除客户
    //http://localhost:8080/customer/delete
    @PostMapping("/delete")
    public ServiceVO delete(String[] ids){
        try {
            customerService.delete(ids);
            return new ServiceVO(SuccessCode.SUCCESS_CODE,SuccessCode.SUCCESS_MESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new ServiceVO(ErrorCode.PARA_TYPE_ERROR_CODE, ErrorCode.PARA_TYPE_ERROR_MESS);
        }
    }

    //http://localhost:8080/customer/getComboboxList
    //销售出库  客户下拉列表
    @PostMapping("/getComboboxList")
    public List<Customer> getComboboxList(String q){
        return customerService.getComboboxList(q);
    }
}
